package com.sherllinhe.business.mq;

import com.alibaba.fastjson.JSON;
import com.sherllinhe.business.dto.ConfirmOrderMQDdto;
import com.sherllinhe.business.req.ConfirmOrderDoReq;
import com.sherllinhe.business.service.ConfirmOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 功能：
 * 作者：Sherllin He
 * 日期：2025/7/10 23:30
 */
@Service
@RocketMQMessageListener(topic = "CONFIRM_ORDER", consumerGroup = "default")
@Slf4j
public class ConfirmOrderConsumer implements RocketMQListener<MessageExt> {

    @Autowired
    private ConfirmOrderService confirmOrderService;
    @Override
    public void onMessage(MessageExt messageExt) {
        byte[] body = messageExt.getBody();
        ConfirmOrderMQDdto dto = JSON.parseObject(new String(body), ConfirmOrderMQDdto.class);;
        MDC.put("LOG_ID", dto.getLogId());
        log.info("ROCKETMQ收到消息:{}", dto);
        try {
            confirmOrderService.doConfirm(dto);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
